package com.bubbo.serviceCenterImpl;

import com.bubbo.rpc.client.util.CollectionUtil;
import com.bubbo.rpc.client.util.zkConstent;
import com.bubbo.serviceCenter.ServiceRegister;
import org.I0Itec.zkclient.ZkClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by waasdasd on 2017/4/1.
 */
public class ServiceRegisterImpl implements ServiceRegister {
    private static final Logger logger = LoggerFactory.getLogger(ServiceRegisterImpl.class);

    private final ZkClient zkClient;


    public ServiceRegisterImpl(String address) {
        this.zkClient = new ZkClient(address, 5000, 1000);
        logger.debug("connect.......");
    }

    @Override
    public void register(String serviceName, String serviceAddress) {
        String registerNode = zkConstent.ZK_REGISTRY_PATH;
        if (!zkClient.exists(registerNode)) {
            zkClient.createPersistent(registerNode);
            logger.debug("create node:" + registerNode);
        }
        String serviceNode = registerNode + "/" + serviceName;
        if (!zkClient.exists(serviceNode)) {
            zkClient.createPersistent(serviceNode);
            logger.debug("create serviceNode:" + serviceNode);
        }
        String providerPath = serviceNode + "/providers";
        zkClient.createEphemeral(providerPath, serviceAddress);
    }
}
